<template>
  <div class="account-login">
    <van-nav-bar
      title="登录"
      @click-left="$router.go(-1)"
      left-text="返回"
      left-arrow
    >
      <template #left>
        <van-icon name="arrow-left" />
        <span class="van-icon">返回</span>
      </template>
    </van-nav-bar>
    <div class="bg-fff box-login mt-5 flex jc-c">
      <div class="login-w">
        <div class="mt-10">
          <div class="flex login-bor">
            <van-field
              v-model="phone"
              placeholder="请输入手机号码"
              type="tel"
            /><input
              class="login-inp"
              type="button"
              :value="test"
              @click="getCode"
            />
          </div>
          <div class="login-bor">
            <van-field
              v-model="smsCode"
              placeholder="请输入短信验证码"
              type="tel"
            />
          </div>
        </div>
        <div class="flex jc-c mt-20">
          <van-button type="primary" @click="getSmsCode">短信验证</van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $http from "@/utils/request";
export default {
  data() {
    return {
      phone: "13811111111",
      smsCode: "",
      test: "获取验证码",
      count: 5,
      counting: false,
    };
  },
  methods: {
    //获取验证码
    getCode() {
      if (this.counting) {
        return false;
      }
      let phoneRag = /^1[3456789]\d{9}$/;
      if (!phoneRag.test(this.phone)) {
        this.$toast("手机号码不正确，请重新输入");
        return false;
      }
      this.countDom();
      let data = { phone: this.phone };
      $http.post("/user/getSmsCode", data).then((res) => {
        this.smsCode = res.smsCode;
      });
    },
    //倒计时
    countDom() {
      this.counting = true;
      let timer = setInterval(() => {
        this.test = this.count--;
        if (this.count === 0) {
          this.test = "重新获取验证码";
          this.count = 5;
          clearInterval(timer);
          this.counting = false;
        }
      }, 1000);
    },
    //登录
    getSmsCode() {
      let phoneRag = /^1[3456789]\d{9}$/;
      if (!phoneRag.test(this.phone)) {
        this.$toast("手机号码不正确，请重新输入");
        return false;
      }
      // 检查验证码是否是空的
      if (!this.smsCode) {
        this.$toast("请输入验证码");
        return false;
      }
      let data = { phone: this.phone, smsCode: this.smsCode };
      $http.post("/user/login", data).then((res) => {
        this.$store.commit('SET_PHONE',this.phone);

        this.$store.commit('set_token',res.result.token);
        //哪里来回哪里
        this.$router.go(-1);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar .van-icon {
  color: #c03131;
}
.van-button--primary {
  color: #fff;
  background-color: #c03131;
  border: 1px solid #c03131;
}
.van-button {
  width: 300px;
}
html,
body {
  height: 100%;
  margin: 0px;
  padding: 0px;
}
.account-login {
  background-color: #999;
  height: 100%;
}
.box-login {
  width: 100%;
  position: absolute;
  top: 46px;
  bottom: 0px;
  left: 0px;
}
.login-inp {
  border: none;
  background: #fff;
}
.van-cell::after {
  border-bottom: none;
}
.login-bor {
  border-bottom: 1px solid #999;
}
</style>
.
